<?php

namespace App\Admin\Controllers;

use App\Models\Points;
use App\Models\User;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Widgets;
use Illuminate\Http\Request;

class PointsController extends Controller
{
    use ModelForm;

    public $header = '积分查询';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Points::class, function (Grid $grid) {

            $grid->model()->orderBy('user_id', 'desc');
            $grid->id('编号');
            $grid->user()->id('用户编号')->sortable();
            $grid->user()->username('用户名称');
            $grid->user()->mobile('手机号');
            $grid->points('积分');
            $grid->column('修改数值')->openNote(function () {
            }, '修改数值');

            $grid->disableCreateButton();
            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();

                $actions->append('<a href="'.$actions->getResource().'/'.$actions->row->user_id.'"><i class="fa fa-eye"></i>积分明细</a>');
            });


            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('user.username', '用户名称');
                $filter->like('user.mobile', '手机号');
            });
        });
    }

    public function updatePoints(Request $request)
    {
        $rules = [
            'user_id' => 'required',
            'radio_points' => 'required',
            'points' => 'required|min:1',
        ];

        $params = $this->validate($request,$rules);

        $user_id = $request->user_id;
        $radio_points = $request->radio_points;
        $points = $request->points;
        $note = $request->note;

        $user = User::find($user_id);

        //用户是否存在
        if ($user){
            $detail = new Points\Detail;
            $detail->user_id = $user_id;
            $detail->points = $radio_points == '1' ? $points : $points * (-1);
            $detail->operator_id = Admin::user()->id;
            $detail->note = $note;

            $status = $detail->save();

            if ($status) {
                $points_model = Points::where('user_id', $user_id)->first();
                if ($radio_points == '1'){
                    $points_model->increment('points', $points);
                } else {
                    $points_model->decrement('points', $points);
                }
                $status = $points_model->save();

            }

        } else {
            $status = false;
            $msg = '用户不存在';
        }

        return response()->json([
            'status'  => $status,
            'message' => $msg ?? '修改成功',
        ]);

    }

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('积分明细');
            $content->description('description');

            $user = User::where('id', $id)->with('points')->first()->toArray();

            $headers = ['用户Id', '用户昵称', '手机号', '性别', '身份', '当前校币','经验值'];
            $rows = [
                [
                    $user['id'],
                    $user['username'],
                    $user['mobile'],
                    $user['gender'] == '0' ? '女' : ($user['gender'] == '1') ? '男' : '保密',
                    $user['is_householder'] == 1 ? '户主' : '',
                    $user['points']['points'],
                    $user['points']['updated_at']
                ],
            ];
            $table = new Widgets\Table($headers, $rows);
            $box1 = new Widgets\Box($user['username'], $table);
            $content->row($box1->solid()->style('primary'));

            $grid = Admin::grid(Points\Detail::class, function (Grid $grid) use ($id) {

                $grid->model()->where([
                    ['user_id',$id],
                ])->orderBy('user_id','desc');
                $grid->id('id');
                $grid->type('积分来源')->using([
                    '1' => '管理员修改',
                    '2' => '点评'
                ]);
                $grid->points('积分变化')->display(function ($v){
                    return $v > 0 ? '+' . $v : $v;
                });
                $grid->created_at('时间');
                $grid->note('备注');

                $grid->disableCreateButton();
                $grid->disableActions();

                $grid->disableRowSelector();

                //搜索栏
                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    //$filter->like('money','校币来源');
                });
            });

            $box2 = new Widgets\Box('积分明细', $grid->render());
            $content->row($box2->solid()->style('primary'));

        });
    }


}
